home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / muiiconupdate / mui_icon_update.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  31KB  |  782 lines

  1. /* MUI_Icon_Update.c
  2.    An application for use with MagicWB.
  3.    Copyright © 1994 by Robert Poole, all rights reserved
  4. */
  5.  
  6. /* MUI */
  7. #include <libraries/mui.h>
  8.  
  9. /* System */
  10. #include <dos.h>
  11. #include <dos/dos.h>
  12. #include <dos/dostags.h>
  13. #include <graphics/gfxmacros.h>
  14. #include <workbench/workbench.h>
  15. #include <exec/memory.h>
  16.  
  17. /* Prototypes */
  18. #include <clib/alib_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/icon_protos.h>
  22. #include <clib/graphics_protos.h>
  23. #include <clib/intuition_protos.h>
  24. #include <clib/gadtools_protos.h>
  25. #include <clib/muimaster_protos.h>
  26. #include <clib/asl_protos.h>
  27.  
  28. /* ANSI C */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <math.h>
  32.  
  33. /* pragmas */
  34. #include <pragmas/muimaster_pragmas.h>
  35.  
  36. /* version string */
  37. #define MYVER "1.2"
  38.  
  39. extern struct IntuitionBase *IntuitionBase;
  40. extern struct GfxBase *GfxBase;
  41. extern struct Library *UtilityBase,*DOSBase,*IconBase,*AslBase,*IconBase,
  42.         *WorkbenchBase, *GadToolsBase;
  43. static struct Library *MUIMasterBase = NULL;
  44.  
  45. /* function prototypes */
  46. static VOID fail(APTR, char *);
  47. static VOID init(VOID);
  48. __saveds __asm LONG AppMsgFunc(register __a2 APTR, register __a1 struct AppMessage **);
  49. #ifdef FUTURE
  50. __saveds __asm LONG AppMsgFunc2(register __a2 APTR, register __a1 struct AppMessage **);
  51. #endif
  52. APTR Create_Window(struct DiskObject *dobj);
  53. struct Image *Augment_Image(struct Image *);
  54. void Free_Augmented_Image(struct Image *);
  55. __saveds __asm APTR source_rxfunc(register __a0 struct Hook *,
  56.         register __a2 Object *, register __a1 ULONG *);
  57. __saveds __asm APTR target_rxfunc(register __a0 struct Hook *,
  58.         register __a2 Object *, register __a1 ULONG *);
  59. char *IconName(char *);
  60.  
  61. /*************************/
  62. /* Init & Fail Functions */
  63. /*************************/
  64.  
  65. static VOID fail(APTR app,char *str)
  66. {
  67.     if (str) {
  68.       /* This *should* work OK -- if app is NULL, it'll default to
  69.          a standard system requester */
  70.       if (MUIMasterBase) {
  71.         MUI_Request(app, NULL, 0, NULL, "OK", str);
  72.       }
  73.       else {
  74.         BPTR fh;
  75.  
  76.         /* write str to a CONsole */
  77.         fh = Open("CON:0/0/400/100/Error Message/AUTO/WAIT", MODE_NEWFILE);
  78.         Write(fh, str, strlen(str));
  79.         Close(fh);
  80.       }
  81.     }
  82.  
  83.     if (app)
  84.         MUI_DisposeObject(app);
  85.  
  86.     if (MUIMasterBase)
  87.         CloseLibrary(MUIMasterBase);
  88.  
  89.     if (str) {
  90.       exit(20);
  91.     }
  92.     else {
  93.       exit(0);
  94.     }
  95. }
  96.  
  97. static VOID init(VOID)
  98. {
  99.     if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  100.         fail(NULL,"Failed to open "MUIMASTER_NAME".");
  101. }
  102.  
  103.  
  104.  
  105. /* Hook function to put dropped icons into string gadget */
  106. __saveds __asm LONG AppMsgFunc(register __a2 APTR obj, register __a1 struct AppMessage **x)
  107. {
  108.         struct WBArg *ap;
  109.         struct AppMessage *amsg = *x;
  110.         int i;
  111.         static char buf[256];
  112.         
  113.         for (ap = amsg->am_ArgList, i = 0; i < amsg->am_NumArgs; i++, ap++) {
  114.                 NameFromLock(ap->wa_Lock, buf, sizeof(buf));
  115.                 AddPart(buf, ap->wa_Name, sizeof(buf));
  116.                 /* if it's a directory, clobber '/' at end */
  117.                 if (buf[strlen(buf) - 1] == '/') {
  118.                         buf[strlen(buf) - 1] = '\0';
  119.                 }
  120.                 set(obj, MUIA_String_Contents, buf);
  121.         }
  122.         return(0);
  123. }
  124.  
  125. static struct Hook AppMsgFuncHook = {
  126.         {NULL, NULL}, (VOID *) AppMsgFunc, NULL, NULL
  127. };
  128.  
  129.  
  130. /* Hook function to put dropped icons into image object */
  131. #ifdef FUTURE
  132. __saveds __asm LONG AppMsgFunc2(register __a2 APTR obj, register __a1 struct AppMessage **x)
  133. {
  134.         struct WBArg *ap;
  135.         struct AppMessage *amsg = *x;
  136.         int i;
  137.         static char buf[256];
  138.         struct DiskObject *temp;
  139.         
  140.         set(obj, MUIA_ShowMe, FALSE);
  141.         for (ap = amsg->am_ArgList, i = 0; i < amsg->am_NumArgs; i++, ap++) {
  142.                 NameFromLock(ap->wa_Lock, buf, sizeof(buf));
  143.                 AddPart(buf, ap->wa_Name, sizeof(buf));
  144.                 /* Now do a GetDiskObjectNew()... */
  145.                 temp = GetDiskObjectNew(buf);
  146.                 set(obj, MUIA_Image_OldImage, temp->do_Gadget.GadgetRender);
  147.         }
  148.         set(obj, MUIA_ShowMe, TRUE);
  149.         return(0);
  150. }
  151.  
  152. static struct Hook AppMsgFuncHook2 = {
  153.         {NULL, NULL}, (VOID *) AppMsgFunc2, NULL, NULL
  154. };
  155. #endif
  156.  
  157. #ifndef MAKE_ID
  158. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  159. #endif
  160.  
  161. int CXBRK(void) { return(0); }
  162. int _CXBRK(void) { return(0); }
  163. void chkabort(void) {}
  164.  
  165. LONG __stack = 20000L;
  166. char *__procname = "MUI_Icon_Update_process";
  167. long __oslibversion = 37L;
  168.  
  169. static Object *App;      /* Application object */
  170. static Object *WI_main;
  171. static Object *CK_Tool_Types;   /* Preserve tooltypes? */
  172. static Object *CK_Icon_Position;        /* Preserve icon position? */
  173. static Object *CK_Stack_Size;   /* Preserve stack size? */
  174. static Object *CK_Default_Tool; /* Preserve default tool? */
  175. static Object *CK_Window_Position;      /* Preserve window position? */
  176. static Object *CK_Repair_Structure;     /* Repair icon to display correctly? */
  177. static Object *PO_Source;       /* source icon */
  178. static Object *ST_Source;
  179. static Object *IM_Source;       /* source image */
  180. static Object *PO_Target;       /* target icon */
  181. static Object *ST_Target;
  182. static Object *IM_Target;       /* target image */
  183. static Object *BT_Start;        /* start the conversion */
  184.  
  185. typedef enum {ID_dummy, ID_start, ID_about, ID_quit, ID_perform,
  186.         ID_results} IDVal;
  187.  
  188. /* Hook functions for ARexx */
  189. __saveds __asm APTR source_rxfunc(register __a0 struct Hook *hook,
  190.         register __a2 Object *app, register __a1 ULONG *arg)
  191. {
  192.         char *fname;
  193.         
  194.         if (fname = (char *)*arg) {
  195.                 set(ST_Source, MUIA_String_Contents, fname);
  196.         }
  197.  
  198.         return(RETURN_OK);
  199. }
  200.  
  201. static struct Hook source_rxhook = {
  202.         {NULL, NULL}, (void *) source_rxfunc, NULL, NULL
  203. };
  204.  
  205. __saveds __asm APTR target_rxfunc(register __a0 struct Hook *hook,
  206.         register __a2 Object *app, register __a1 ULONG *arg)
  207. {
  208.         char *fname;
  209.         
  210.         if (fname = (char *)*arg) {
  211.                 set(ST_Target, MUIA_String_Contents, fname);
  212.         }
  213.  
  214.         return(RETURN_OK);
  215. }
  216.  
  217. static struct Hook target_rxhook = {
  218.         {NULL, NULL}, (void *) target_rxfunc, NULL, NULL
  219. };
  220.  
  221. __saveds __asm APTR query_rxfunc(register __a0 struct Hook *hook,
  222.         register __a2 Object *app, register __a1 ULONG *arg)
  223. {
  224.         char *gadget;
  225.         ULONG checked;
  226.         Object *temp;
  227.         
  228.         if (gadget = (char *)*arg) {
  229.                 switch (gadget[0]) {
  230.                         case 't':
  231.                         case 'T':
  232.                         temp = CK_Tool_Types;
  233.                         break;
  234.                         
  235.                         case 'i':
  236.                         case 'I':
  237.                         temp = CK_Icon_Position;
  238.                         break;
  239.                         
  240.                         case 's':
  241.                         case 'S':
  242.                         temp = CK_Stack_Size;
  243.                         break;
  244.                         
  245.                         case 'd':
  246.                         case 'D':
  247.                         temp = CK_Default_Tool;
  248.                         break;
  249.                         
  250.                         case 'w':
  251.                         case 'W':
  252.                         temp = CK_Window_Position;
  253.                         break;
  254.                         
  255.                         case 'r':
  256.                         case 'R':
  257.                         temp = CK_Repair_Structure;
  258.                         break;
  259.                         
  260.                         default:
  261.                         return(RETURN_OK);
  262.                         break;
  263.                 }
  264.                 get(temp, MUIA_Selected, &checked);
  265.                 set(app, MUIA_Application_RexxString,
  266.                     (checked ? "true" : "false"));
  267.         }
  268.         
  269.         return(RETURN_OK);
  270. }
  271.  
  272. static struct Hook query_rxhook = {
  273.         {NULL, NULL}, (void *) query_rxfunc, NULL, NULL
  274. };
  275.  
  276. __saveds __asm APTR toggle_rxfunc(register __a0 struct Hook *hook,
  277.         register __a2 Object *app, register __a1 ULONG *arg)
  278. {
  279.         char *gadget;
  280.         ULONG checked;
  281.         Object *temp;
  282.         
  283.         if (gadget = (char *)*arg) {
  284.                 switch (gadget[0]) {
  285.                         case 't':
  286.                         case 'T':
  287.                         temp = CK_Tool_Types;
  288.                         break;
  289.                         
  290.                         case 'i':
  291.                         case 'I':
  292.                         temp = CK_Icon_Position;
  293.                         break;
  294.                         
  295.                         case 's':
  296.                         case 'S':
  297.                         temp = CK_Stack_Size;
  298.                         break;
  299.                         
  300.                         case 'd':
  301.                         case 'D':
  302.                         temp = CK_Default_Tool;
  303.                         break;
  304.                         
  305.                         case 'w':
  306.                         case 'W':
  307.                         temp = CK_Window_Position;
  308.                         break;
  309.                         
  310.                         case 'r':
  311.                         case 'R':
  312.                         temp = CK_Repair_Structure;
  313.                         break;
  314.                         
  315.                         default:
  316.                         return(RETURN_OK);
  317.                         break;
  318.                 }
  319.                 get(temp, MUIA_Selected, &checked);
  320.                 set(temp, MUIA_Selected, !checked);
  321.                 set(app, MUIA_Application_RexxString,
  322.                     (!checked ? "true" : "false"));
  323.         }
  324.         
  325.         return(RETURN_OK);
  326. }
  327.  
  328. static struct Hook toggle_rxhook = {
  329.         {NULL, NULL}, (void *) toggle_rxfunc, NULL, NULL
  330. };
  331.  
  332.  
  333. #ifdef SLEEP_IMAGE
  334. extern struct DiskObject sleepimg;
  335. #endif
  336.  
  337. extern struct DiskObject drophere;
  338. static struct DiskObject *source, *target, *synthetic;
  339.  
  340. /* Menus for all windows. */
  341. static struct NewMenu menu_list[] =
  342. {
  343.         {NM_TITLE, "Project", 0, 0, 0, 0
  344.         },
  345.         {NM_ITEM,  "Perform", "P", 0, 0, (APTR) ID_start
  346.         },
  347.         {NM_ITEM,  NM_BARLABEL, 0, 0, 0, 0
  348.         },
  349.         {NM_ITEM,  "About",   "?", 0, 0, (APTR) ID_about
  350.         },
  351.         {NM_ITEM,  NM_BARLABEL, 0, 0, 0, 0
  352.         },
  353.         {NM_ITEM,  "Quit",    "Q", 0, 0, (APTR) ID_quit
  354.         },
  355.         {NM_TITLE, "Settings", 0, 0, 0, 0
  356.         },
  357.         {NM_ITEM,  "Display Results?", 0, CHECKIT|CHECKED|MENUTOGGLE, 0,
  358.                 (APTR) ID_results},
  359.         {NM_END,   NULL, 0, 0, 0, 0
  360.         },
  361. };
  362.  
  363. /* ARexx commands */
  364. static struct MUI_Command arexx_list[] =
  365. {
  366.         {"perform", MC_TEMPLATE_ID, ID_perform, NULL
  367.         },
  368.         {"source", "SOURCEFILE/A", 1, &source_rxhook
  369.         },
  370.         {"target", "TARGETFILE/A", 1, &target_rxhook
  371.         },
  372.         {"toggle", "GADNAME/A", 1, &toggle_rxhook
  373.         },
  374.         {"query",  "GADNAME/A", 1, &query_rxhook
  375.         },
  376.         {NULL, NULL, 0, NULL
  377.         }
  378. };
  379.  
  380. int main(void)
  381. {
  382.         BOOL running = TRUE, uses_rexx = FALSE, show_results = TRUE;
  383.         ULONG signal = 0, status = 0, active = 0, checked = 0;
  384.         char *fname, *fname_icon;
  385.         BPTR file_lock;
  386.         struct FileInfoBlock *fib;
  387.         LONG oldbits;
  388.         
  389.         init();
  390.         
  391.         fib = AllocDosObjectTags(DOS_FIB,
  392.                                  TAG_DONE);
  393.         
  394.         /* start creating the application */
  395.         App = ApplicationObject,
  396.                 MUIA_Application_Title,         "Icon_Update",
  397.                 MUIA_Application_Version,       "$VER: MUI Icon Update "MYVER" "__AMIGADATE__,
  398.                 MUIA_Application_Copyright,     "Copyright © 1994 Robert Poole",
  399.                 MUIA_Application_Author,        "Robert Poole",
  400.                 MUIA_Application_Description,   "MagicWB icon update / repair program",
  401.                 MUIA_Application_Base,          "ICONUPD",
  402.                 MUIA_Application_Menu,          menu_list,
  403.                 MUIA_Application_Commands,      arexx_list,
  404. #ifdef SLEEP_IMAGE
  405.                 MUIA_Application_DiskObject,    &sleepimg,
  406. #else
  407.                 MUIA_Application_DiskObject,    &drophere,
  408. #endif
  409.                 SubWindow, WI_main = Create_Window(&drophere),
  410.                 End;
  411.         if (!App) fail(App, "Failed to create application.");
  412.         
  413.         /* close window when close gadget is hit */
  414.         DoMethod(WI_main, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, App, 2,
  415.                  MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  416.         /* automagically put name of file into string gad when icon dropped on image */
  417.         DoMethod(IM_Source, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime,
  418.                  ST_Source, 3, MUIM_CallHook, &AppMsgFuncHook, MUIV_TriggerValue);
  419.         DoMethod(IM_Target, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime,
  420.                  ST_Target, 3, MUIM_CallHook, &AppMsgFuncHook, MUIV_TriggerValue);
  421.  
  422. #ifdef FUTURE
  423.         /* automagically put image of icon into image object when dropped */
  424.         DoMethod(IM_Source, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime,
  425.                  IM_Source, 3, MUIM_CallHook, &AppMsgFuncHook2, MUIV_TriggerValue);
  426.         DoMethod(IM_Target, MUIM_Notify, MUIA_AppMessage, MUIV_EveryTime,
  427.                  IM_Target, 3, MUIM_CallHook, &AppMsgFuncHook2, MUIV_TriggerValue);
  428. #endif
  429.  
  430.         /* when iconified, target gets messages from icons dropped on
  431.            AppIcon */
  432.         set(App, MUIA_Application_DropObject, IM_Target);
  433.  
  434.         /* return value for START button */
  435.         DoMethod(BT_Start, MUIM_Notify, MUIA_Pressed, FALSE, App, 2,
  436.                  MUIM_Application_ReturnID, ID_start);
  437.         
  438.         /* open the window */
  439.         set(WI_main, MUIA_Window_Open, TRUE);
  440.         get(WI_main, MUIA_Window_Open, &status);
  441.         if (!status) {
  442.                 fail(App, "Failed to open window.");
  443.         }
  444.         
  445.         while (running) {
  446.                 switch (DoMethod(App, MUIM_Application_Input, &signal)) {
  447.                         case ID_quit:
  448.                         case MUIV_Application_ReturnID_Quit:
  449.                         get(PO_Source, MUIA_Popasl_Active, &active);
  450.                         if (!active) get(PO_Target, MUIA_Popasl_Active, &active);
  451.                         
  452.                         if (active) {
  453.                                 MUI_Request(App, WI_main, 0, NULL, "OK",
  454.                                             "Can't quit, ASL popups still open.");
  455.                         }
  456.                         else {
  457.                                 running = FALSE;
  458.                         }
  459.                         break;
  460.                         
  461.                         
  462.                         case ID_about:
  463.                         MUI_Request(App, WI_main, 0, NULL, "OK",
  464.                                     "MUI Icon Update\n\n"
  465.                                     "Version "MYVER"\n"
  466.                                     "Compiled "__DATE__"\n"
  467.                                     "Copyright © 1994 Robert Poole\n\n"
  468.                                     "MUI is Copyright © 1993, 1994\n"
  469.                                     "by Stefan Stuntz",
  470.                                     TAG_END);
  471.                         break;
  472.                         
  473.                         case ID_results:
  474.                         show_results = (!show_results);
  475.                         break;
  476.                         
  477.                         case ID_perform:
  478.                         uses_rexx = TRUE;
  479.  
  480.                         /* falls through to... */
  481.  
  482.                         case ID_start:
  483.                         get(ST_Source, MUIA_String_Contents, &fname);
  484.                         if (strlen(fname) == 0)
  485.                                 break;
  486.                         source = GetDiskObjectNew(fname);
  487.                         if (source == NULL) {
  488.                                 MUI_Request(App, WI_main, 0, NULL, "OK",
  489.                                             "Can't read source.");
  490.                                 break;
  491.                         }
  492.                         get(ST_Target, MUIA_String_Contents, &fname);
  493.                         if (strlen(fname) == 0) {
  494.                                 FreeDiskObject(source);
  495.                                 break;
  496.                         }
  497.                         /* We want to be able to update icons of
  498.                            write-protected files. */
  499.                         fname_icon = IconName(fname);
  500.                         file_lock = Lock(fname_icon, ACCESS_READ);
  501.                         if (file_lock == NULL) {
  502.                                 fname_icon = fname;
  503.                                 file_lock = Lock(fname, ACCESS_READ);
  504.                                 if (file_lock == NULL) {
  505.                                         /* tell user we can't do it */
  506.                                         MUI_Request(App, WI_main, 0, NULL, "OK",
  507.                                                     "Can't obtain file lock.");
  508.                                         FreeDiskObject(source);
  509.                                         break;
  510.                                 }
  511.                         }
  512.                         if (Examine(file_lock, fib)) {
  513.                                 oldbits = fib->fib_Protection;
  514.                                 if (oldbits & FIBF_WRITE) {
  515.                                         if (!SetProtection(fname_icon, oldbits & (~FIBF_WRITE))) {
  516.                                                 MUI_Request(App, WI_main, 0, NULL, "OK",
  517.                                                         "Can't reset Write bit.");
  518.                                         }
  519.                                 }
  520.                         }
  521.                         else {
  522.                                 oldbits = -1;
  523.                                 MUI_Request(App, WI_main, 0, NULL, "OK",
  524.                                             "Warning: Can't examine protection bits.");
  525.                         }
  526.                         target = GetDiskObjectNew(fname);
  527.                         synthetic = (struct DiskObject *) AllocMem(sizeof(struct DiskObject),
  528.                                                         MEMF_PUBLIC | MEMF_CLEAR);
  529.                         synthetic->do_Magic = source->do_Magic;
  530.                         synthetic->do_Version = source->do_Version;
  531.                         synthetic->do_Gadget = source->do_Gadget;
  532.                         synthetic->do_Type = source->do_Type;
  533.                         get(CK_Tool_Types, MUIA_Selected, &checked);
  534.                         if (checked) {
  535.                                 synthetic->do_ToolTypes = target->do_ToolTypes;
  536.                         }
  537.                         else {
  538.                                 synthetic->do_ToolTypes = source->do_ToolTypes;
  539.                         }
  540.                         get(CK_Icon_Position, MUIA_Selected, &checked);
  541.                         if (checked) {
  542.                                 synthetic->do_CurrentX = target->do_CurrentX;
  543.                                 synthetic->do_CurrentY = target->do_CurrentY;
  544.                         }
  545.                         else {
  546.                                 synthetic->do_CurrentX = NO_ICON_POSITION;
  547.                                 synthetic->do_CurrentY = NO_ICON_POSITION;
  548.                         }
  549.                         get(CK_Stack_Size, MUIA_Selected, &checked);
  550.                         if (checked) {
  551.                                 synthetic->do_StackSize = target->do_StackSize;
  552.                         }
  553.                         else {
  554.                                 synthetic->do_StackSize = 8192;
  555.                         }
  556.                         get(CK_Default_Tool, MUIA_Selected, &checked);
  557.                         if (checked) {
  558.                                 synthetic->do_DefaultTool = target->do_DefaultTool;
  559.                         }
  560.                         else {
  561.                                 synthetic->do_DefaultTool = source->do_DefaultTool;
  562.                         }
  563.                         get(CK_Window_Position, MUIA_Selected, &checked);
  564.                         if (checked) {
  565.                                 synthetic->do_DrawerData = target->do_DrawerData;
  566.                         }
  567.                         else {
  568.                                 synthetic->do_DrawerData = NULL;
  569.                         }
  570.                         get(CK_Repair_Structure, MUIA_Selected, &checked);
  571.                         if (checked) {
  572.                                 synthetic->do_Gadget.GadgetRender =
  573.                                         Augment_Image(source->do_Gadget.GadgetRender);
  574.                                 if ((source->do_Gadget.Flags) & GFLG_GADGHIMAGE) {
  575.                                         synthetic->do_Gadget.SelectRender =
  576.                                                 Augment_Image(source->do_Gadget.SelectRender);
  577.                                 }
  578.                         }
  579.                         /* Before we PutDiskObject(), let's Unlock() */
  580.                         UnLock(file_lock);
  581.                         /* now write out the icon */
  582.                         if (PutDiskObject(fname, synthetic)) {
  583.                                 if (!uses_rexx && show_results) {
  584.                                         MUI_Request(App, WI_main, 0, NULL, "OK",
  585.                                                 "Update Successful.");
  586.                                 }
  587.                                 else {
  588.                                         uses_rexx = FALSE;
  589.                                 }
  590.                         }
  591.                         else {
  592.                                 MUI_Request(App, WI_main, 0, NULL, "OK",
  593.                                         "Error!  Can't update icon.");
  594.                         }
  595.                         /* now reset the protection bits */
  596.                         if (oldbits != -1) {
  597.                                 SetProtection(fname_icon, oldbits);
  598.                         }
  599.                         
  600.                         /* OK, if we have "repaired" the imagery, we must
  601.                            deallocate resources. */
  602.                         if (checked) {
  603.                                 Free_Augmented_Image(synthetic->do_Gadget.GadgetRender);
  604.                                 if ((source->do_Gadget.GadgetType) & GFLG_GADGHIMAGE) {
  605.                                         Free_Augmented_Image(synthetic->do_Gadget.SelectRender);
  606.                                 }
  607.                         }
  608.                         /* now free up the synthetic disk object */
  609.                         FreeMem((void *) synthetic, sizeof(struct DiskObject));
  610.                         /* and free the DiskObjects */
  611.                         FreeDiskObject(source);
  612.                         FreeDiskObject(target);
  613.                         break;
  614.                         
  615.                         default:
  616.                         break;
  617.                 }
  618.  
  619.                 if (running && signal) {
  620.                         Wait(signal);
  621.                 }
  622.         }
  623.         
  624.         FreeDosObject(DOS_FIB, fib);
  625.         fail(App, NULL);
  626. }
  627. /* End: main() */
  628.  
  629. APTR Create_Window(struct DiskObject *dobj)
  630. {
  631.         return (
  632.         WindowObject,
  633.                         MUIA_Window_ID,         MAKE_ID('I','C','O','U'),
  634.                         MUIA_Window_Title,      "MUI Icon Update",
  635.                         MUIA_Window_AppWindow,  TRUE,
  636.                         WindowContents, HGroup,
  637.                                 Child, VGroup,
  638.                                         Child, HGroup,
  639.                                                 Child, RectangleObject, End,
  640.                                                 Child, IM_Source = ImageObject,
  641.                                                         TextFrame,
  642.                                                         MUIA_Image_OldImage, dobj->do_Gadget.GadgetRender,
  643.                                                         End,
  644.                                                 Child, RectangleObject, End,
  645.                                                 End,
  646.                                         Child, ColGroup(2),
  647.                                                 Child, KeyLabel2("Source:",'s'),
  648.                                                 Child, PO_Source = PopaslObject,
  649.                                                         MUIA_Popstring_String, ST_Source = KeyString(0, 256, 's'),
  650.                                                         MUIA_Popstring_Button, PopButton(MUII_PopFile),
  651.                                                         ASLFR_TitleText, "Please select source icon",
  652.                                                         ASLFR_DoPatterns, TRUE,
  653.                                                         ASLFR_InitialPattern, "~(#?.info)",
  654.                                                         End,
  655.                                                 End,
  656.                                         Child, HGroup,
  657.                                                 Child, RectangleObject, End,
  658.                                                 Child, IM_Target = ImageObject,
  659.                                                         TextFrame,
  660.                                                         MUIA_Image_OldImage, drophere.do_Gadget.GadgetRender,
  661.                                                         End,
  662.                                                 Child, RectangleObject, End,
  663.                                                 End,
  664.                                         Child, ColGroup(2),
  665.                                                 Child, KeyLabel2("Target:",'t'),
  666.                                                 Child, PO_Target = PopaslObject,
  667.                                                         MUIA_Popstring_String, ST_Target = KeyString(0, 256, 't'),
  668.                                                         MUIA_Popstring_Button, PopButton(MUII_PopFile),
  669.                                                         ASLFR_TitleText, "Please select target icon",
  670.                                                         ASLFR_DoPatterns, TRUE,
  671.                                                         ASLFR_InitialPattern, "~(#?.info)",
  672.                                                         End,
  673.                                                 End,
  674.                                         Child, HGroup,
  675.                                                 Child, RectangleObject,
  676.                                                         MUIA_HorizWeight, 300,
  677.                                                         End,
  678.                                                 Child, BT_Start = KeyButton("Start",'r'),
  679.                                                 End,
  680.                                         End,
  681.                                 Child, RectangleObject,
  682.                                         MUIA_Rectangle_VBar,    TRUE,
  683.                                         MUIA_FixWidth,          8,
  684.                                         End,
  685.                                 Child, VGroup,
  686.                                         Child, ColGroup(2),
  687.                                                 MUIA_Frame, MUIV_Frame_None,
  688.                                                 MUIA_FrameTitle, "Preserve:",
  689.                                                 Child, CK_Tool_Types = CheckMark(TRUE),
  690.                                                 Child, LLabel1("Tool Types"),
  691.                                                 Child, CK_Icon_Position = CheckMark(TRUE),
  692.                                                 Child, LLabel1("Icon Position"),
  693.                                                 Child, CK_Stack_Size = CheckMark(TRUE),
  694.                                                 Child, LLabel1("Stack Size"),
  695.                                                 Child, CK_Default_Tool = CheckMark(TRUE),
  696.                                                 Child, LLabel1("Default Tool"),
  697.                                                 Child, CK_Window_Position = CheckMark(TRUE),
  698.                                                 Child, LLabel1("Window Position"),
  699.                                                 End,
  700.                                         Child, RectangleObject,
  701.                                                 MUIA_Rectangle_HBar,    TRUE,
  702.                                                 MUIA_FixHeight,         6,
  703.                                                 End,
  704.                                         Child, ColGroup(2),
  705.                                                 Child, CK_Repair_Structure = CheckMark(FALSE),
  706.                                                 Child, LLabel1("Repair Icon Structure"),
  707.                                                 End,
  708.                                         End,
  709.                                 End,
  710.                         End
  711.                 );
  712. }
  713. /* End: CreateWindow() */
  714.  
  715. /* The following function takes a struct Image as input and returns a
  716.    similar structure that has been augmented to display correctly under
  717.    Amigados 3.0.  This is accomplished by taking bitplane 2 and dupli-
  718.    cating it to create bitplanes 3 through 7. */
  719.  
  720. struct Image *Augment_Image(struct Image *source)
  721. {
  722.         struct Image *temp;
  723.         ULONG planesize, copydepth;
  724.         register ULONG i;
  725.         
  726.         /* step 1 -- allocate a temp image structure */
  727.         temp = (struct Image *) AllocMem(sizeof(struct Image),
  728.                                          MEMF_PUBLIC | MEMF_CLEAR);
  729.         /* step 2 -- start copying contents of source to temp */
  730.         temp->LeftEdge = source->LeftEdge;
  731.         temp->TopEdge = source->TopEdge;
  732.         temp->Width = source->Width;
  733.         temp->Height = source->Height;
  734.         /* we are creating an 8-bitplane image */
  735.         temp->Depth = 8;
  736.         /* as such, we change PlanePick to be what we want */
  737.         temp->PlanePick = 0x00FF;
  738.         temp->PlaneOnOff = 0x0000;
  739.         temp->NextImage = NULL;
  740.         
  741.         /* step 3 -- create an array of UWORD the size we need */
  742.         planesize = ((source->Width + 15) / 16) * (source->Height);
  743.         temp->ImageData = (UWORD *) AllocVec(sizeof(UWORD) * planesize * 8,
  744.                                              MEMF_CHIP | MEMF_CLEAR);
  745.         /* step 4 -- copy bitplanes that exist */
  746.         copydepth = (source->Depth > 3 ? 3 : source->Depth);
  747.         for (i = 0; i < planesize * copydepth; i++) {
  748.                 temp->ImageData[i] = source->ImageData[i];
  749.         }
  750.         /* OK, if copydepth < 3, we're done, otherwise keep going! */
  751.         if (copydepth < 3) {
  752.                 return(temp);
  753.         }
  754.         else {
  755.                 /* duplicate bitplane 2 repeatedly */
  756.                 for (i = 0; i < planesize * 5; i++) {
  757.                         temp->ImageData[i + planesize*3] =
  758.                                 source->ImageData[(i % planesize) + planesize*2];
  759.                 }
  760.                 return(temp);
  761.         }
  762. }
  763. /* end: Augment_Image() */
  764.  
  765. /* anything allocated with Augment_Image() must be deallocated... */
  766. void Free_Augmented_Image(struct Image *img)
  767. {
  768.         FreeVec(img->ImageData);
  769.         FreeMem((void *) img, sizeof(struct Image));
  770. }
  771. /* end: Free_Augmented_Image() */
  772.  
  773. /* Append .info extension to a file name. */
  774. char *IconName(char *fname)
  775. {
  776.         static char buf[256];
  777.         
  778.         strcpy(buf, fname);
  779.         return(strcat(buf, ".info"));
  780. }
  781. /* end: IconName() */
  782.